package start.demo01.fileIO;

import jdk.nashorn.api.scripting.URLReader;
import org.junit.Test;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/*
* File(path:String);以一个指定路径创建一个file对象
* File(parent:String,child:String);在parent目录下创建一个子路径的file对象
* File(parent:File,child:String)
* */
public class FileDemo {
    public static void main(String[] args) throws IOException {
        String path="test.txt";
        String name="scores.txt";
        File f=new File(path);
        System.out.println(f.getAbsolutePath());

//        PrintWriter p=new PrintWriter(f);
//        p.close();

        //write("das.txt");//没有将会创建

        getFileName(new File("D:\\file"));
        //readData(path);
        //String[] s={"ds","sad"};
        //replace(s);
        //writeFile("D:/test.txt");
        //readWriteFileFromUrl("http://www.163.com");
        //write1("fire.dat");
        //readFile("fire.dat");

    }
    public static void write(String path){
        File file=new File(path);
        if(file.exists()){
            System.out.println("文件已存在");
        }else{
            PrintWriter writer=null;
            try {
                //Writer writer = new PrintWriter(file);Writer无print()方法
                writer = new PrintWriter(file);
                /*
                print方法可以将各种类型的数据转换成字符串的形式输出。
                重载的write方法只能输出字符、字符数组、字符串等与字符相关的数据。
                * */
                //writer.write("pasdsd");
                //writer.write(340);
                writer.println("rewr");
                writer.println(43);
                writer.print("你啊\t再见\n电脑");
                writer.close();
                //writer.flush();
            }catch(FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(writer!=null)
                    writer.close();
            }
//            try(
//                    FileWriter write=new FileWriter(file);//自动关流
//                    //只有继承了AtuoColseable接口的类才能写在这里，一般的需要关流的类都可以
//            ){
//                write.append('d');
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            /*JDK1.9自动关流
            * MyInputStream mis = new MyInputStream();
            MyOutputStream mos = new MyOutputStream();
            try (mis; mos) {
                mis.read("1.9");
                mos.write("1.9");
            } catch (Exception e) {
                e.printStackTrace();
            }
            * */
        }
    }

    public static void getFileName(File file){
        if(file.isDirectory()){
            File[] files=file.listFiles();//遍历该目录下的文件和目录,不能递归遍历目录
            for(int i=0;files!=null&&i<files.length;i++){
                System.out.println(files[i].getPath());
            }
        }else{
            System.out.println(file.getPath());
            //return file.getPath();
        }
    }

    public static void readData(String path){
        try(Scanner input =new Scanner(new File(path))) {
            while(input.hasNext()){
                String name=input.next();//遇到分隔符结束这次读取；如\t,\n
                System.out.println(name);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void replace(String[] args){
        if(args==null||args.length!=4){
            System.out.println("参数不符合");
            throw new IllegalArgumentException("参数不符合");
        }
        try(Scanner input = new Scanner(new File(args[0]));
            PrintWriter out=new PrintWriter(args[1]);
        ){
            while(input.hasNext()){
                String src=input.nextLine();
                String dist=src.replace(args[2],args[3]);
                out.print(dist);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void writeFile(String path) {
        try (Writer out = new FileWriter(path)){
            out.write("1\t射雕英雄传\t金庸\t400\t三联出版社");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readWriteFileFromUrl(String path){
        try{
            URL url=new URL(path);

            BufferedReader reader=new BufferedReader(new URLReader(url));
            String line="";
            BufferedWriter out =new BufferedWriter(new FileWriter("index.html"));
            while((line=reader.readLine())!=null){
                out.write(line);
                out.newLine();
            }
            out.close();
            reader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFile(String name) throws IOException{
//        InputStream input=new FileInputStream(name);
//        for(int i=0;i<6;i++) {
//            System.out.println(input.read());//读取一个字节
//        }
//        input.close();
        try(InputStream input=new FileInputStream(name)){
            int ret=-1;
            while((ret=input.read())!=-1){
                System.out.println(ret);
            }
        }
    }

    public static void write1(String path){
        try(OutputStream out=new FileOutputStream(path)) {
            for(int i=0;i<10;i++){
                out.write(i);
                out.write(Integer.parseInt("他是一个香蕉"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File src,File dish) throws IOException{
        try(InputStream in=new FileInputStream(src);
            OutputStream out=new FileOutputStream(dish);){
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=in.read(buffer))!=-1){
                out.write(buffer,0,len);
            }
        }
    }
    @Test
    public void createFileTest() throws IOException {
        String dir="D:\\home/file/test.txt";
        File file=new File(dir);
        boolean file1 = file.createNewFile();
        if(file1) {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write("这是一个测试文件".getBytes(StandardCharsets.UTF_8));
            outputStream.close();
        }
    }

}
